package com.example.bookmall.controller.orderSystem;


import com.example.bookmall.entity.Aftersales;
import com.example.bookmall.entity.order.*;
import com.example.bookmall.entity.Result;
import com.example.bookmall.service.AftersalesService;
import com.example.bookmall.service.OrderService;
import com.example.bookmall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AftersalesService aftersalesService;

    //创建订单 在购物车购买
    @PostMapping("/create")
    public Result createOrder(@RequestBody OrderSubmitDto orderSubmitDto) {

        System.out.println("订单提交数据：" + orderSubmitDto);

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");

        return orderService.createOrder(orderSubmitDto);
    }

    //创建订单 立即购买
    @PostMapping("/Buynow")
    public Result createOrderBuynow(@RequestBody OrderBuyNowDto orderBuyNowDto) {

        System.out.println("订单提交数据：" + orderBuyNowDto);

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");

        return orderService.createOrderBuynow(userId,orderBuyNowDto);
    }


    //获取订单列表 根据用户ID
    @GetMapping("/list")
    public Result getOrdersByUserId() {

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");

        List<OrderVo> orderVo = orderService.getOrderVoByUserId(userId);
        return Result.success(orderVo);
    }


    //获取订单详情 根据订单ID
    @GetMapping("/detail/{orderId}")
    public Result getOrderDetails(@PathVariable String orderId) {
        OrderVo orderVo= orderService.getOrderDetail(orderId);
        return Result.success(orderVo);
    }


    //支付订单
    @PostMapping("/pay/{orderId}")
    public Result payOrder(@PathVariable String orderId) {
        boolean result = orderService.payOrder(orderId);
        if (result) {
            return Result.success("支付成功");
        } else {
            return Result.error("支付失败");
        }
    }

    //取消订单
    @PostMapping("/cancel/{orderId}")
    public Result cancelOrder(@PathVariable String orderId) {
        boolean result = orderService.cancelOrder(orderId);
        if (result) {
            return Result.success("订单取消成功");
        } else {
            return Result.error("订单取消失败");
        }
    }

    //修改订单状态
    @GetMapping("/UpdateOrderStatus")
    public  Result UpdateOrderStatus(String  orderId,Integer status){
        return orderService.UpdateOrderStatus(orderId,status);
    }


    //申请售后 给理由  提交售后申请
    @PostMapping("/aftersales")

    public Result createAftersales(@RequestBody Aftersales aftersales) {
        return aftersalesService.setaftersales(aftersales);

    }

    //确认收货  用户点击确认 修改订单状态为已完成 4
    @PostMapping("/receipt/{orderId}")
    public Result receiptOrder(@PathVariable String orderId) {
        boolean result = orderService.receiptOrder(orderId);
        if (result) {
            return Result.success("收货成功");
        } else {
            return Result.error("收货失败");
        }
    }


    //修改用户订单的地址信息 用户个人  在未收货前都可以进行修改
    @PostMapping("/updateAddress")
    public Result updateOrderAddress(@RequestBody OrderDTO orderDTO) {
        return orderService.UpdateOrderAddress(orderDTO);
    }

    //根据订单id删除订单信息
    @GetMapping("/deleteOrderById")
    public Result deleteOrderById(String orderId){
        return orderService.deleteOrderById(orderId);
    }


}
